package view;

import app.App;
import component.CustomJTable;
import model.User;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class SystemPane {

    private JPanel contentPane;
    private JTable table_user;
    private JButton btn_add;
    private JButton btn_del;
    private JButton btn_mod;

    DefaultTableModel tableModel;

    private void createUIComponents() {
        table_user = new CustomJTable(new String[]{"员工号","姓名","密码","职位"});
        tableModel = (DefaultTableModel) table_user.getModel();
        notifyUserListChanged();
        CustomJTable.resizeColumnWidth(table_user);
    }

    public SystemPane() {
        //添加用户按钮
        btn_add.addActionListener(e -> {
            if (App.checkManagePermission()) {
                new DialogStaffAdd(SystemPane.this).setVisible(true);
            }
            else {
                JOptionPane.showMessageDialog(contentPane, "当前用户没有该权限", "权限不足！", JOptionPane.ERROR_MESSAGE);
            }
        });
        //修改用户按钮
        btn_mod.addActionListener(e -> {
            if (App.checkManagePermission()) {
                int row = table_user.getSelectedRow();
                if (row != -1) {
                    //获得要修改的旧用户
                    User oldUser = App.currentUserList.get(row);
                    new DialogStaffMod(SystemPane.this, oldUser).setVisible(true);
                }
            }
            else {
                JOptionPane.showMessageDialog(contentPane, "当前用户没有该权限", "权限不足！", JOptionPane.ERROR_MESSAGE);
            }
        });
        //删除用户按钮
        btn_del.addActionListener(e -> {
            if (App.checkManagePermission()) {
                int row = table_user.getSelectedRow();
                if (row != -1) {
                    if (JOptionPane.showConfirmDialog(contentPane, "是否确定删除?\n\n(选择多行则只有第一行有效)", "确认删除",
                            JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == 0) {
                        if (User.delUser((Integer) tableModel.getValueAt(row, 0))) {
                            JOptionPane.showMessageDialog(contentPane, "用户删除成功", "删除用户", JOptionPane.INFORMATION_MESSAGE);
                            notifyUserListChanged();
                        } else {
                            JOptionPane.showMessageDialog(contentPane, "用户删除失败", "删除用户", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            }
            else {
                JOptionPane.showMessageDialog(contentPane, "当前用户没有该权限", "权限不足！", JOptionPane.ERROR_MESSAGE);
            }
        });
    }

    public JPanel getContentPane() {
        return contentPane;
    }

    //更新视图
    public void notifyUserListChanged() {
        System.out.println("员工列表更新：");
        App.currentUserList = User.getUsers();

        tableModel.setRowCount(0);
        if (App.currentUserList != null) {
            for (User user : App.currentUserList) {
                tableModel.addRow(toTableObjectArray(user));
            }
        }
    }

    private Object[] toTableObjectArray(User user) {
        return new Object[]{user.getUserId(), user.getUserName(), "******", user.getJobName()};
    }
}
